package xyz.hubery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import xyz.hubery.domain.SysRolePrivilegeUser;
import xyz.hubery.domain.SysUser;
import xyz.hubery.domain.SysUserRole;
import xyz.hubery.mapper.SysUserMapper;
import xyz.hubery.service.SysRolePrivilegeUserService;
import xyz.hubery.service.SysUserRoleService;
import xyz.hubery.service.SysUserService;
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{

    @Autowired
    private SysUserRoleService userRoleService;
    @Autowired
    private SysRolePrivilegeUserService rolePrivilegeUserService;


    @Override
    public void delUsers(List<Long> ids) {
        this.removeByIds(ids);
        // 删除员工角色信息
        userRoleService.remove(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId,ids));
        // 删除用户权限配置信息
        rolePrivilegeUserService.remove(new LambdaQueryWrapper<SysRolePrivilegeUser>().in(SysRolePrivilegeUser::getUserId,ids));
    }

    @Override
    public void updateUser(SysUser user) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        // 1、删除该用户原来的所有角色
        userRoleService.remove(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId,user.getId()));
        // 2、添加用户角色信息
        String role_strings = user.getRole_strings();
        String[] ids = role_strings.split(",");
        List<Long> roleIds = new ArrayList<>();
        for (String id : ids) {
            roleIds.add(Long.valueOf(id));
        }
        userRoleService.addUserRoles(user.getId(),roleIds);
        // 3、判断密码是否变化，变化则加密处理
        // 3.1 查询数据库中原密码
        SysUser orignUser = this.getById(user.getId());
        if(!orignUser.getPassword().equals(user.getPassword())) { // 说明改变密码,加密处理
            if(StringUtils.isEmpty(user.getPassword())) { //没有填密码，则使用原来的密码
                user.setPassword(orignUser.getPassword());
            }else{
                user.setPassword(passwordEncoder.encode(user.getPassword()));
            }
        }
        // 4、更新操作
        this.updateById(user);
    }

    @Override
    public SysUser findUserById(Long userId) {
        SysUser sysUser = this.getById(userId);
        // 查询该用户的角色id列表
        List<SysUserRole> roles = userRoleService.list(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));
        List<Long> roleIds = roles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        StringBuilder sb = new StringBuilder();
        for (Long roleId : roleIds) {
            sb.append(roleId+",");
        }
        sb.deleteCharAt(sb.length()-1);
        sysUser.setRole_strings(sb.toString());
        return sysUser;
    }
}
